package com.apes.fn.business.insurance.modle;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName : InsuranceOrder
 * @Description : 保险订单
 * @Author : WSJ
 * @Date: 2020-05-19 17:20
 */
@Setter
@Getter
@Entity
@SelectBeforeUpdate
@EntityListeners(DefaultPersistListener.class)
@Table(name = InsuranceOrder.CONST_TABLE_NAME)
@DynamicInsert
@DynamicUpdate
public class InsuranceOrder extends Model {
    public static final String CONST_TABLE_NAME = "insurance_order";

    @Id
    @Column(name = "id", nullable = false)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "保险订单", padding = 10,prefix = "1")
    @Comment("保险订单号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept", referencedColumnName = "id")
    @Comment("门店")
    private Dept dept;

    @Comment("车牌")
    @Column(name = "license_Plate")
    private  String licensePlate;

    @Comment("单据状态")
    @Column(name = "state")
    private  String state;

    @Comment("单据状态名")
    @Column(name = "state_name")
    private  String stateName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "salesman", referencedColumnName = "id", nullable = false)
    @Comment("业务员")
    private PartyRole salesman;

    @Column(name = "external_staff", length = 32)
    @Comment("外部业务员")
    private String externalStaff;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier", referencedColumnName = "id", nullable = false)
    @Comment("供应商")
    private PartyRole supplier;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("所属公司")
    private PartyRole company;

    @Comment("收款日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "receipt_date")
    private Date receiptDate;

    @Comment("退款日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "refund_Date")
    private Date refundDate;

    @Comment("退保日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "abolish_Date")
    private Date abolishDate;

    @Comment("商业险退保金额")
    @Column(name = "commercial_abolish_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double commercialAbolishAmount;

    @Comment("交强险退保金额")
    @Column(name = "compulsory_abolish_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double compulsoryAbolishAmount;

    @Comment("驾意险退保金额")
    @Column(name = "driving_abolish_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double drivingAbolishAmount;

    @Comment("退保金额")
    @Column(name = "abolish_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double abolishAmount;

    @Comment("红包金额")
    @Column(name = "red_packet_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double redPacketAmount;

    @Comment("返现金额")
    @Column(name = "rebate_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double rebateAmount;

    @Column(name = "belong_plan", length = 32)
    @Comment("方案所属")
    private String belongPlan;

    @Comment("作废日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "void_Date")
    private Date voidDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "compulsory_Insurance_Start")
    @Comment("交强险起保时间")
    private Date compulsoryInsuranceStart;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "compulsory_Insurance_End")
    @Comment("交强险终保时间")
    private Date compulsoryInsuranceEnd;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "commercial_Insurance_Start")
    @Comment("商业险起保时间")
    private Date commercialInsuranceStart;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "commercial_Insurance_End")
    @Comment("商业险终保时间")
    private Date commercialInsuranceEnd;

    @Comment("商业险金额")
    @Column(name = "commercial_Insurance", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double commercialInsurance;

    @Comment("交强险金额")
    @Column(name = "compulsory_Insurance", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double compulsoryInsurance;

    @Comment("车船税")
    @Column(name = "car_Ship_Tax", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double carShipTax;

    @Comment("油卡金额")
    @Column(name = "oil_Card_Amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private Double oilCardAmount;

    @Column(name = "origin", length = 32)
    @Comment("来源")
    private String origin;

    @Column(name = "discount_Type", length = 32)
    @Comment("优惠类型")
    private String discountType;

    @Column(name = "discount_Type_Name", length = 32)
    @Comment("优惠类型描述")
    private String discountTypeName;

    @Column(name = "discount_Plan", length = 32)
    @Comment("优惠方案")
    private String discountPlan;

    @Column(name = "discount_Plan_Name", length = 32)
    @Comment("优惠方案描述")
    private String discountPlanName;

    @Column(name = "APP_Order", length = 32)
    @Comment("APP订单")
    private String APPOrder;

    @Column(name = "car_insurance_sale_fee", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("车险销售管理费")
    private double insuranceManagementFee = 0;

    @Column(name = "driving_Insurance_Amount",  columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("驾意险金额")
    private double drivingInsuranceAmount;

    @Column(name = "driving_Insurance_Name", length = 64)
    @Comment("驾意险名称")
    private String drivingInsuranceName;

    @Column(name = "e_Maintain", length = 64)
    @Comment("e服务类型(1:e保养;2:e轮胎)")
    private String eMaintain;

    @OneToMany(mappedBy = "insuranceOrder",cascade = CascadeType.ALL,orphanRemoval = true)
    private Set<InsuranceOrderItem> insuranceOrderItems = new HashSet<>();

    public Set<InsuranceOrderItem> getInsuranceOrderItems() {
        if (insuranceOrderItems == null) return new HashSet<>();
        for (InsuranceOrderItem item : insuranceOrderItems) {
            item.setInsuranceOrder(this);
        }
        return insuranceOrderItems;
    }
}
